<template>
  <div ref="containerRef" class="error-container">
    <!-- 404 数字 -->
    <div class="error-code animate-bounce mb-8">404</div>

    <!-- 错误信息 -->
    <div class="mb-10">
      <h1 class="error-title">页面走丢了 🚀</h1>
      <p class="error-desc">
        抱歉，您访问的页面不存在。可能是地址输入错误，或者它已经乘坐火箭去太空旅行了。
      </p>
    </div>

    <!-- 操作按钮 -->
    <div class="error-actions">
      <button @click="goBack" class="action-btn btn-back">返回上一页</button>
      <button @click="goHome" class="action-btn btn-home">返回首页</button>
    </div>

    <!-- 装饰性元素 -->
    <div class="decoration">
      <div class="planet"></div>
      <div class="ufo">
        <div class="ufo-light"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 导入 useRouter
import { useRouter } from 'vue-router'
import { onMounted, useTemplateRef } from 'vue'

// 创建 router 实例
const router = useRouter()

// 获取容器的引用，用于动画
const containerRef = useTemplateRef('containerRef')

// 返回上一页
const goBack = () => {
  // 尝试返回上一页
  router.go(-1)
  // 如果无法返回（比如在第一个页面），则跳转到首页
  setTimeout(() => {
    if (router.currentRoute.value.path === '/404') {
      router.push('/')
    }
  }, 500)
}

// 返回首页
const goHome = () => {
  router.push('/')
}

// 组件挂载后执行进入动画
onMounted(() => {
  const container = containerRef.value
  if (container) {
    // 初始状态
    container.style.opacity = '0'
    container.style.transform = 'translateY(20px)'

    // 触发重排，确保初始样式应用

    // container.offsetHeight

    // 添加过渡并恢复最终状态
    container.style.transition = 'all 0.6s ease'
    container.style.opacity = '1'
    container.style.transform = 'translateY(0)'
  }
})
</script>

<style scoped>
/* 整体容器 */
.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  color: white;
  text-align: center;
  padding: 20px;
  box-sizing: border-box;
}

/* 404 数字样式 */
.error-code {
  font-size: 120px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  letter-spacing: 4px;
}

/* 动画：让 404 数字轻微弹跳 */
.animate-bounce {
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%,
  20%,
  50%,
  80%,
  100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-15px);
  }
  60% {
    transform: translateY(-10px);
  }
}

.error-title {
  font-size: 36px;
  margin-bottom: 15px;
  font-weight: 600;
}

.error-desc {
  font-size: 18px;
  color: rgba(255, 255, 255, 0.8);
  max-width: 500px;
  line-height: 1.6;
}

/* 操作按钮区域 */
.error-actions {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  justify-content: center;
}

/* 通用按钮样式 */
.action-btn {
  padding: 12px 30px;
  border: none;
  border-radius: 50px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  outline: none;
}

/* 返回上一页按钮 */
.btn-back {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
}

.btn-back:hover {
  background-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

/* 返回首页按钮 */
.btn-home {
  background-color: #ff6b6b;
  color: white;
}

.btn-home:hover {
  background-color: #ee5253;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
}

/* 装饰性元素 */
.decoration {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  height: 100px;
  pointer-events: none;
}

.planet {
  position: absolute;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle at 30% 30%, #4a90e2, #1e3a8a);
  border-radius: 50%;
  bottom: 0;
  left: 30%;
  box-shadow:
    inset -10px -10px 20px rgba(0, 0, 0, 0.5),
    0 0 30px rgba(74, 144, 226, 0.5);
}

.ufo {
  position: absolute;
  width: 60px;
  height: 30px;
  background: linear-gradient(to bottom, #dfe9f3, #a9c1d9);
  border-radius: 50% 50% 40% 40%;
  bottom: 80px;
  left: 60%;
  animation: fly-horizontal 8s infinite ease-in-out;
  /* UFO 主体 */
}

.ufo::before {
  content: '';
  position: absolute;
  width: 70px;
  height: 15px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  bottom: -10px;
  left: -5px;
  /* UFO 的碟状底座 */
}

.ufo-light {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #ffeb3b;
  border-radius: 50%;
  bottom: -15px;
  left: 10px;
  box-shadow:
    15px 0 0 #ffeb3b,
    30px 0 0 #ffeb3b;
  animation: blink 1s infinite;
  /* UFO 底部的灯光 */
}

@keyframes fly-horizontal {
  0%,
  100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(100px);
  }
}

@keyframes blink {
  0%,
  50% {
    opacity: 1;
  }
  51%,
  100% {
    opacity: 0.4;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .error-code {
    font-size: 80px;
  }
  .error-title {
    font-size: 28px;
  }
  .error-desc {
    font-size: 16px;
  }
  .action-btn {
    padding: 10px 20px;
    font-size: 14px;
  }
  .decoration {
    bottom: 20px;
  }
  .planet,
  .ufo {
    transform: scale(0.8);
  }
}
</style>
